//
//  IGIssueDelImageViewController.m
//  jingdongfang
//
//  Created by BOE on 2017/8/16.
//  Copyright © 2017年 kevinLY. All rights reserved.
//

#import "IGIssueDelImageViewController.h"
#import "IGIssueDelImageCell.h"

static NSString *issueDelImageCellIdentifier = @"issueImageDelCellIdentifier";

@interface IGIssueDelImageViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
{
    NSMutableArray *_assets;
    
    NSIndexPath *_indexPath;
}

@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation IGIssueDelImageViewController

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, NavigationMagin, KSCREENWIDTH, KSCREENHEIGHT) collectionViewLayout:flowLayout];
        _collectionView.height = _collectionView.height - NavigationMagin;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.pagingEnabled = YES;
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.alwaysBounceVertical = YES;
        [_collectionView registerClass:[IGIssueDelImageCell class] forCellWithReuseIdentifier:issueDelImageCellIdentifier];
        
        flowLayout.sectionInset = UIEdgeInsetsZero;
        flowLayout.itemSize = _collectionView.size;
        flowLayout.minimumLineSpacing = 0;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    }
    return _collectionView;
}

- (instancetype)initWithAssets:(NSArray *)assets andImageIndexPath: (NSIndexPath *)indexPath {
    if (self = [super init]) {
        _assets = [NSMutableArray arrayWithArray:assets];
        _indexPath = indexPath;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor mainBackgroundColor];

    [self.view addSubview:self.collectionView];
    [_collectionView setContentOffset:CGPointMake(_indexPath.row * self.view.width, 0)];
    
//    [self ig_setupRightButtonItemWithTitle:@"删除" image:nil highLightedImage:nil];
    [self ig_setupRightButtonItemWithTitle:NSLocalizedString(@"Delete", nil) image:nil highLightedImage:nil];
}

- (void)ig_rightButtonPressed:(id)sender {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:NSLocalizedString(@"Delete", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        double page = _collectionView.contentOffset.x / _collectionView.width;
        NSInteger currentIndex = (int)(page + 0.5);
        
        [_assets removeObjectAtIndex:currentIndex];
        [self.collectionView reloadData];
        self.title = [NSString stringWithFormat:@"%@/%@", @(currentIndex + 1), @(_assets.count)];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"selectedImageDelNotification" object:self userInfo:@{@"assets": _assets}];
        
        if (_assets.count == 0) {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertVC addAction:action1];
    [alertVC addAction:action2];
    [self presentViewController:alertVC animated:YES completion:nil];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _assets.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    IGIssueDelImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:issueDelImageCellIdentifier forIndexPath:indexPath];
    cell.imageView.image = _assets[indexPath.item];

    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    double page = scrollView.contentOffset.x / scrollView.width;
    NSInteger currentIndex = (int)(page + 0.5);
    
    self.title = [NSString stringWithFormat:@"%@/%@", @(currentIndex + 1), @(_assets.count)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
